<%@ page import="java.sql.*" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>删除留言</title>
  </head>
  <body>

  <%
    try{
      System.out.println("删除界面正在加载。");
      String name = (String) session.getAttribute("name");
      System.out.println("当前用户为："+name);
      try {
        Class.forName("com.mysql.jdbc.Driver");
      } catch (ClassNotFoundException e) {
        e.printStackTrace();
      }
      Connection conn =DriverManager.getConnection("jdbc:mysql://localhost:3306/library", "root", "20030607");
      Statement stat = conn.createStatement();
      Statement stat1 = conn.createStatement();
      String num = request.getParameter("num");//获取留言编号
      String sql1 = "SELECT pid FROM message WHERE messagenum ='"+num+"'";
      ResultSet rs = stat1.executeQuery(sql1);
      while(rs.next()){
        if(rs.getString(1).equals(name)){
          num = new String(num.getBytes());
          System.out.print("num="+num);
          String sql = "DELETE FROM message WHERE messagenum ='"+num+"';";
          int i = stat.executeUpdate(sql);
          System.out.print("成功删除数据，编号为"+num);
          response.setHeader("refresh", "3;URL=addmessage.jsp");//三秒后返回留言界面
          System.out.print("当前留言属于用户："+rs.getString(1));
          out.print("<div class='box'><div class='delete'>已删除！</div>" + "<div class='delete'>3秒后返回</div></div>");
        }else{
          out.print("<div class='box'><div class='delete'>不能删除其他用户的留言哦！</div>" + "<div class='delete'>3秒后返回</div></div>");
          response.setHeader("refresh", "3;URL=addmessage.jsp");
        }
      }
    }catch (SQLException e){
      System.out.print(e);
    }
  %>
  </body>
<style>
  body{
    margin: 0;
  }
  .box{
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    background: linear-gradient(#e66465, #9198e5);
  }
  .delete{
    font-size: 2em;
    color: azure;
  }
</style>
</html>
</html>